import Vue from 'vue'
import App from './App.vue'
import store from './store'
import API from './api/index'
import cookie from 'js-cookie'
import router from './router'
import { message, Modal, Icon, Result, Layout, Menu, Card, Button, Breadcrumb, Form, Input, Table, Switch, Pagination, Tabs } from 'ant-design-vue'

Vue.use(Icon).use(Result).use(Layout).use(Menu).use(Card).use(Button).use(Breadcrumb).use(Form).use(Input).use(Table).use(Switch).use(Pagination).use(Tabs)

Vue.prototype.$api = API

const serverUrl = 'http://49.235.166.90:9527'

if (location.search.includes('token')) {
  const token = location.search.split('=')[1]
  cookie.set('token', token, { expires: 1, path: '' })
  getUser(token)
  router.replace({ query: {} })
} else if (cookie.get('token')) {
  getUser(cookie.get('token'))
} else {
  setTimeout(() => {
    router.push('/403')
  }, 0)
}

function getUser (token) {
  API.yibanApi.getUserInfo({ token }).then(res => {
    if (res.status === 'success') {
      store.state.userInfo = res.info
      API.report.getConfig({ type: 'permissionsConfig' }).then(res => {
        if (res.code === 0) {
          if (res.data[0]) {
            const userId = store.state.userInfo.yb_userid
            const result = res.data[0].data.filter(ele => ele.userId === userId)
            if (!result.length) {
              router.push('/403')
            } else if (location.search.includes('token')) {
              location.href = location.origin + '/adm/#/StuList'
            }
          }
        }
      })
    } else {
      // 请求失败，token无效清除token
      cookie.remove('token')
      window.location.href = serverUrl + '/login'
    }
  })
}

Vue.config.productionTip = false

Vue.prototype.$message = message
Vue.prototype.$confirm = Modal.confirm

new Vue({
  store,
  router,
  render: h => h(App)
}).$mount('#app')
